//
//  HeartRateZoneModel.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/24.
//

import UIKit
import MYModule

public enum HeartRateZoneType: CaseIterable {
    
    case light
    case weight
    case aerobic
    case anaerobic
    case Vo2Max
}

extension HeartRateZoneType {
    
    public var title: String {
        
        switch self {
        case .light:
            return "heart_rate_light_range".language
        case .weight:
            return "heart_rate_weight_range".language
        case .aerobic:
            return "heart_rate_aerobic_range".language
        case .anaerobic:
            return "heart_rate_anaerobic_range".language
        case .Vo2Max:
            return "heart_rate_max_range".language
            
        }
    }
    
    
    public var tintColor: UIColor {
        switch self {
        case .light:
            return UIColor.HeartRate.light
        case .weight:
            return UIColor.HeartRate.weight
        case .aerobic:
            return UIColor.HeartRate.aerobic
        case .anaerobic:
            return UIColor.HeartRate.anaerobic
        case .Vo2Max:
            return UIColor.HeartRate.Vo2Max
            
        }
    }
    
    
    /// 区间最小最大值
    /// 注意： 判断一个值区间 是 min <= value < max
    /// - Parameter rateMax: 最大心率
    /// - Returns: 元组
    public func heartRange(of rateMax: CGFloat) -> Range<CGFloat> {
        
        switch self {
        case .light:
            return rateMax * 0.5 ..< rateMax * 0.6
        case .weight:
            return rateMax * 0.6 ..< rateMax * 0.7
        case .aerobic:
            return rateMax * 0.7 ..< rateMax * 0.8
        case .anaerobic:
            return rateMax * 0.8 ..< rateMax * 0.9
        case .Vo2Max:
            return rateMax * 0.9 ..< CGFloat.greatestFiniteMagnitude
        }
        
    }
    
}

open class HeartRateZoneSubModel: NSObject {
    
    /// 类型
    public var type: HeartRateZoneType = .light
    
    /// 秒
    public var second: Int = 0
    
    
}

open class HeartRateZoneModel: NSObject {
    
    public var subModels: [HeartRateZoneSubModel] {
        
        return [self.light, self.weight, self.aerobic, self.anaerobic, self.Vo2Max]
    }
    
    public var totalSecond: Int = 0
    
    public var isEmpty: Bool {
        
        return subModels.filter { $0.second > 0 }.isEmpty
        
    }
    
    public var hour: Int {
        return totalSecond/3600
        
    }
    
    
    public var minute: Int {
        return (totalSecond - hour * 3600)/60
    }
    
    public var second: Int {
        
        return totalSecond - hour * 3600 - minute * 60
    }
    
    public var light: HeartRateZoneSubModel = {
        
        let model = HeartRateZoneSubModel()
        model.type = .light
        return model
    }()
    
    public var weight: HeartRateZoneSubModel = {
        
        let model = HeartRateZoneSubModel()
        model.type = .weight
        return model
    }()
    
    public var aerobic: HeartRateZoneSubModel = {
        
        let model = HeartRateZoneSubModel()
        model.type = .aerobic
        return model
    }()
    
    public var anaerobic: HeartRateZoneSubModel = {
        
        let model = HeartRateZoneSubModel()
        model.type = .anaerobic
        return model
    }()
    
    public var Vo2Max: HeartRateZoneSubModel = {
        
        let model = HeartRateZoneSubModel()
        model.type = .Vo2Max
        return model
    }()

}


extension HeartRateZoneModel {
    
    /// 计算心率区间分钟数
    /// - Parameters:
    ///   - hearts: 心率数组
    ///   - heartRateInterval: 心率间隔
    ///   - componet: 心率间隔单位
    /// - Returns: 心率区间 model
   public static func calculationMinute(with hearts: [Int], heartRateInterval: Int, componet: Calendar.Component) ->HeartRateZoneModel {
        
       let model = HeartRateZoneModel()
       let subModel = model.subModels
       // 统一用秒来算
       
       // 默认 10秒
       var secondInterval = 10
       
       if componet == .second {
           
           secondInterval = heartRateInterval
           
       } else if componet == .minute {
           
           secondInterval = heartRateInterval * 60
           
       } else if componet == .hour {
           
           secondInterval = heartRateInterval * 3600
       }
       
       let userModel = UserCacheManagement.userInfo()
       let rate = CGFloat(userModel.defaultMaxHR)
       
       let lightRange = HeartRateZoneType.light.heartRange(of: rate)
       let weightRange = HeartRateZoneType.weight.heartRange(of: rate)
       let aerobicRange = HeartRateZoneType.aerobic.heartRange(of: rate)
       let anaerobicRange = HeartRateZoneType.anaerobic.heartRange(of: rate)
       let Vo2MaxRange = HeartRateZoneType.Vo2Max.heartRange(of: rate)
       
      
       hearts.forEach { heart in
           
           if heart == 0 {
               return
           }
           
           if lightRange.contains(CGFloat(heart)) {
               model.light.second += secondInterval
               return
           }
           if weightRange.contains(CGFloat(heart)) {
               model.weight.second += secondInterval
               return
           }
           if aerobicRange.contains(CGFloat(heart)) {
               model.aerobic.second += secondInterval
               return
           }
           if anaerobicRange.contains(CGFloat(heart)) {
               model.anaerobic.second += secondInterval
               return
           }
           
           if Vo2MaxRange.contains(CGFloat(heart)) {
               model.Vo2Max.second += secondInterval
               return
           }
          
       }
       
       // 计算总时间
       let totalSecond = subModel.reduce(0, { partialResult, model in
           return partialResult + model.second
       })
       
       model.totalSecond = totalSecond
     
       return model
    }
}
